<?php
declare (strict_types=1);

namespace app\admin\service\base;

use app\admin\model\base\User;
use app\admin\model\iot\ScanBagOrder;
use app\BaseService;
use think\response\Json;

/**
 * 用户管理服务层
 */
class UserService extends BaseService
{

    /**
     * 用户列表
     * @param array $params
     * @return Json
     */
    public function userList(array $params): Json
    {
        $where = [];
        //搜索Openid
        if ($params['openid']) {
            $where[] = ['openid', '=', $params['openid']];
        }
        //搜索名称
        if ($params['nickname']) {
            $where[] = ['nickname', 'like', '%' . $params['nickname'] . '%'];
        }
        //搜索手机号
        if ($params['phone']) {
            $where[] = ['phone', '=', $params['phone']];
        }
        //搜索订阅状态
        if (!is_null($params['subscribe']) && in_array($params['subscribe'], [0, 1])) {
            $where[] = ['subscribe', '=', $params['subscribe']];
        }
        //搜索性别
        if (!is_null($params['sex']) && in_array($params['sex'], [0, 1, 2])) {
            $where[] = ['sex', '=', $params['sex']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $data = User::getList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 取袋记录
     * @param array $params
     * @return Json
     */
    public function userBagList(array $params): Json
    {
        $where = [];
        //搜索mac_id
        if ($params['mac_id']) {
            $where[] = ['a.mac_id', '=', $params['mac_id']];
        }
        //搜索订单号
        if ($params['order_no']) {
            $where[] = ['a.order_no', '=', $params['order_no']];
        }
        //搜索Openid
        if ($params['openid']) {
            $where[] = ['b.openid', '=', $params['openid']];
        }
        //搜索订单状态
        if ($params['order_state']) {
            $where[] = ['a.order_state', '=', $params['order_state']];
        }
        //创建时间筛选
        if ($params['start_time'] && $params['end_time']) {
            $start_time = (int)$params['start_time'];
            $end_time = (int)$params['end_time'];
            $where[] = ['a.create_time', 'between', [$start_time, $end_time]];
        }
        //获取列表
        $orderby = 'a.id desc';
        $field = 'a.id,a.order_no,b.openid,b.unionid,b.nickname,b.avatar,a.mac_id,a.order_state,a.pay_pattern,a.pay_type,a.money,a.integral,a.create_time';
        $data = ScanBagOrder::getList($where, $params['page'], $params['limit'], $orderby, $field);
        return jsonReturn(1, $data);
    }

}
